package pers.qianyu.module.system.controller;


import cn.hutool.core.util.*;
import cn.hutool.crypto.asymmetric.*;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.data.redis.core.*;
import org.springframework.validation.annotation.*;
import org.springframework.web.bind.annotation.*;
import pers.qianyu.module.core.comm.*;
import pers.qianyu.module.core.constants.TokenConstants;
import pers.qianyu.module.core.domain.system.dto.*;
import pers.qianyu.module.core.domain.system.group.*;
import pers.qianyu.module.core.domain.system.query.*;
import pers.qianyu.module.core.domain.system.vo.*;
import pers.qianyu.module.core.util.StringUtil;
import pers.qianyu.module.logging.annotation.*;
import pers.qianyu.module.logging.enums.*;
import pers.qianyu.module.system.service.*;

import javax.servlet.http.*;

/**
 * @author mizzle rain
 * @date 2020-11-12 14:42
 */
@Api(tags = "系统用户有关接口")
@RestController
@RequestMapping("/system-serv")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @ApiOperation("根据 id 查询系统用户信息（包括权限、角色信息）")
    @GetMapping("/user/{id}")
    public HttpResult<SysUserVO> queryById(@PathVariable Long id) {
        SysUserVO sysUserVO = sysUserService.queryById(id);
        return HttpResult.okWithData(sysUserVO);
    }

    @ApiOperation("根据 id 查询系统用户信息（不包括权限、角色信息）")
    @GetMapping("/simple_user/{id}")
    public HttpResult<SimpleUserVO> querySimpleUserById(@PathVariable Long id) {
        SimpleUserVO simpleUserVO = sysUserService.querySimpleUserInfoById(id);
        // 对邮箱测手机号进行特殊处理
        simpleUserVO.setEmail(StringUtil.handleEmail(simpleUserVO.getEmail()));
        simpleUserVO.setPhone(StringUtil.handlePhone(simpleUserVO.getPhone()));
        return HttpResult.okWithData(simpleUserVO);
    }

    @ApiOperation("根据用户名查询系统用户是否存在")
    @GetMapping("/username")
    public HttpResult<Boolean> ifUsernameExist(@RequestParam("name") String username) {
        boolean exist = sysUserService.ifUsernameExist(username);
        return HttpResult.okWithData(exist);
    }

    @ApiOperation("新增系统用户")
    @PostMapping("/user")
    public HttpResult<?> insert(@Validated(SysUserInsertValidationGroup.class)
                                @RequestBody SysUserDTO sysUserDTO) {
        sysUserService.insUser(sysUserDTO);
        return HttpResult.okWithMessage("新增用户成功");
    }

    @ApiOperation("根据id删除系统用户")
    @DeleteMapping("/user/{id}")
    public HttpResult<?> delete(@PathVariable Long id) {
        sysUserService.delUser(id);
        return HttpResult.okWithMessage("删除用户成功");
    }

    @ApiOperation("根据系统用户id更新用户信息")
    @PutMapping("/user/{id}")
    public HttpResult<?> updateById(@Validated @PathVariable Long id,
                                    @RequestBody SysUserDTO sysUserDTO) {
        sysUserService.updUser(id, sysUserDTO);
        return HttpResult.okWithMessage("更新成功");
    }

    @ApiOperation("根据系统用户id更新用户信息（普通用户）")
    @PostMapping("/update_user_comm")
    public HttpResult<?> updateUserBySelf(@Validated @RequestBody SysUserForUpdateDTO sysUserForUpdateDTO) {
        sysUserService.updUser(sysUserForUpdateDTO);
        return HttpResult.okWithMessage("更新成功");
    }

    @ApiOperation("分页条件查询")
    @GetMapping("/users")
    public HttpResult<Pagination<SysUserVO>> query(SysUserQuery sysUserQuery) {
        Pagination<SysUserVO> pagination = sysUserService.queryUser(sysUserQuery);
        return HttpResult.okWithData(pagination);
    }

    @ControllerLog(businessType = BusinessType.OTHER)
    @GetMapping("/public_key")
    public HttpResult<?> getPublicKey(HttpServletResponse res) {
        String uuid = IdUtil.fastSimpleUUID();
        res.setHeader("uuid", uuid);
        // RSA 加密
        RSA rsa = new RSA();
        redisTemplate.opsForValue().set(TokenConstants.REGISTER_UUID_PREFIX + uuid,
                rsa.getPrivateKeyBase64(), TokenConstants.CODE_EXPIRE_TIME);
        res.setHeader("sign", rsa.getPublicKeyBase64());
        return HttpResult.okWithMessage("获取成功");
    }

    @ControllerLog(businessType = BusinessType.ADD)
    @PostMapping("/register")
    public HttpResult<?> register(@RequestBody RegisterDTO registerDTO) {
        sysUserService.register(registerDTO);
        return HttpResult.okWithMessage("注册成功成功");
    }
}
